<template>
  <div class="home">
    <anchor content="网站总览" />
    <div class="card-wrap">
      <div class="item">
        <i-box title="会员" subtitle="日" :content="dayCount" sub-content="新会员" color="background: #19be6b"></i-box>
      </div>
      <div class="item">
        <i-box title="会员" subtitle="周" :content="weekCount" sub-content="新会员" color="background: #5cadff"></i-box>
      </div>
      <div class="item">
        <i-box title="会员" subtitle="月" :content="monthCount" sub-content="新会员" color="background: cadetblue"></i-box>
      </div>
      <div class="item">
        <i-box title="会员" subtitle="年" :content="yearCount" sub-content="新会员" color="background: red"></i-box>
      </div>
    </div>
    <anchor content="用户活跃度" />
    <div class="chart">
      <ChartLine />
    </div>
    <anchor content="最新信息" />
    <div class="infos">
      <div class="info-cont">
        <home-collapse :system="systemInfo" :users="newUsers" />
      </div>
    </div>
  </div>
</template>

<script>
import IBox from '../../components/ibox/ibox'
import Anchor from '../../components/anchor/anchor'
import HomeCollapse from './home-collapse'
import ChartLine from './user-chart-line'

import {ADMIN_URL} from '../../config/urls'
export default {
  data () {
    return {
      dayCount: '',
      weekCount: '',
      monthCount: '',
      yearCount: '',
      systemInfo: [],
      newUsers: []
    }
  },
  async created () {
    let date = new Date()
    // let before = date.setHours(0, 0, 0, 0)
    let week = new Date(date - 7000 * 60 * 60 * 24)
    let month = new Date(date - 30000 * 60 * 60 * 24)
    let year = new Date(date - 365000 * 60 * 60 * 24)
    try {
      this.dayCount = await this.getUserNumber(date, date)
      this.weekCount = await this.getUserNumber(week, date)
      this.monthCount = await this.getUserNumber(month, date)
      this.yearCount = await this.getUserNumber(year, date)
    } catch (e) {
      console.error(e)
    }

    this.getSystemInfo()
      .then(data => {
        this.systemInfo = data
      })
      .catch(err => console.log(err))

    this.getNewUserInfo(4)
      .then(data => {
        this.newUsers = data
      })
      .catch(err => console.error(err))
  },
  methods: {
    getUserNumber (start, end) {
      return new Promise((resolve, reject) => {
        this.$http.get(ADMIN_URL.usersNum + `${start}/${end}`)
          .then(data => resolve(data.data.data))
          .catch(err => reject(err))
      })
    },
    getSystemInfo () {
      return new Promise((resolve, reject) => {
        this.$http.get(ADMIN_URL.systemInfo)
          .then(data => resolve(data.data.data))
          .catch(err => reject(err))
      })
    },
    getNewUserInfo (num) {
      return new Promise((resolve, reject) => {
        this.$http.get(ADMIN_URL.newUserInfo + num)
          .then(data => resolve(data.data.data))
          .catch(err => reject(err))
      })
    }
  },
  components: {
    IBox,
    Anchor,
    ChartLine,
    HomeCollapse
  }
}
</script>

<style lang="less" scoped>
  @import "../../common/less/variable";
  .home{
    .card-wrap{
      display: flex;
      background-color: @color-background-sub;
      padding: 40px 20px;
      .item {
        flex: 1;
        margin-right: 20px;
        &:last-child{
          margin-right: 0;
        }
      }
    }
    .infos{
      /*min-height: 500px;*/
      padding: 40px 20px;
      background-color: @color-background-sub;
      .info-cont{
        background-color: #fff;
      }
    }
  }
</style>
